package com.pcafs.manage.controller;

import com.pcafs.common.annotation.Log;
import com.pcafs.common.config.PcafsConfig;
import com.pcafs.common.core.controller.BaseController;
import com.pcafs.common.core.domain.AjaxResult;
import com.pcafs.common.core.page.TableDataInfo;
import com.pcafs.common.enums.BusinessType;
import com.pcafs.common.utils.file.FileUploadUtils;
import com.pcafs.common.utils.poi.ExcelUtil;
import com.pcafs.manage.bo.TeacherInfoAddBo;
import com.pcafs.manage.bo.TeacherInfoEditBo;
import com.pcafs.manage.bo.TeacherInfoQueryBo;
import com.pcafs.manage.domain.TeacherInfo;
import com.pcafs.manage.service.ITeacherInfoService;
import com.pcafs.manage.vo.TDetail;
import com.pcafs.manage.vo.TeacherInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 教师信息Controller
 * 
 * @author pcafs
 * @date 2021-05-02
 */
@Api(value = "教师信息控制器", tags = {"教师信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/manage/teacherInfo")
public class TeacherInfoController extends BaseController {

    private final ITeacherInfoService iTeacherInfoService;

    /**
     * 查询教师信息列表
     */
    @ApiOperation("查询教师信息列表")
    @PreAuthorize("@ss.hasPermi('manage:teacherInfo:list')")
    @GetMapping("/list")
    public TableDataInfo<TeacherInfoVo> list(TeacherInfoQueryBo bo) {
        startPage();
        List<TeacherInfoVo> list = iTeacherInfoService.queryList(bo);
        return getDataTable(list);
    }

    @ApiOperation("查询教师信息列表")
    @PreAuthorize("@ss.hasPermi('manage:teacherInfo:list')")
    @GetMapping("/listVo")
    public TableDataInfo<TeacherInfo> listVo(TeacherInfo teacherInfo) {
        startPage();
        List<TeacherInfo> list = iTeacherInfoService.queryListVo(teacherInfo);
        return getDataTable(list);
    }

    /**
     * 查询教师信息列表
     * @return
     */
    @GetMapping("/teacherList")
    public AjaxResult<List<TeacherInfo>> teacherList() {
        return AjaxResult.success(iTeacherInfoService.list());
    }

    /**
     * 获取教师详情
     *
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('manage:teacherInfo:query')")
    @GetMapping("/getTDetail/{id}")
    public AjaxResult<TDetail> getTDetail(@PathVariable("id") Long id) {
        return AjaxResult.success(iTeacherInfoService.selectTDetail(id));
    }

    /**
     * 导出教师信息列表
     */
    @ApiOperation("导出教师信息列表")
    @PreAuthorize("@ss.hasPermi('manage:teacherInfo:export')")
    @Log(title = "教师信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<TeacherInfoVo> export(TeacherInfoQueryBo bo) {
        List<TeacherInfoVo> list = iTeacherInfoService.queryList(bo);
        ExcelUtil<TeacherInfoVo> util = new ExcelUtil<TeacherInfoVo>(TeacherInfoVo.class);
        return util.exportExcel(list, "教师信息");
    }

    /**
     * 获取教师信息详细信息
     */
    @ApiOperation("获取教师信息详细信息")
    @PreAuthorize("@ss.hasPermi('manage:teacherInfo:query')")
    @GetMapping("/{id}")
    public AjaxResult<TeacherInfoVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iTeacherInfoService.queryById(id));
    }

    /**
     * 新增教师信息
     */
    @ApiOperation("新增教师信息")
    @PreAuthorize("@ss.hasPermi('manage:teacherInfo:add')")
    @Log(title = "教师信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody TeacherInfoAddBo bo) {
        return toAjax(iTeacherInfoService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改教师信息
     */
    @ApiOperation("修改教师信息")
    @PreAuthorize("@ss.hasPermi('manage:teacherInfo:edit')")
    @Log(title = "教师信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody TeacherInfoEditBo bo) {
        return toAjax(iTeacherInfoService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除教师信息
     */
    @ApiOperation("删除教师信息")
    @PreAuthorize("@ss.hasPermi('manage:teacherInfo:remove')")
    @Log(title = "教师信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        return toAjax(iTeacherInfoService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @Log(title = "教师头像", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file, @RequestParam("tId") Long stuId) throws IOException {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(PcafsConfig.getAvatarPath(), file);
            TeacherInfo teacherInfo = new TeacherInfo();
            teacherInfo.setId(stuId);
            teacherInfo.setAvatar(avatar);
            if (iTeacherInfoService.updateById(teacherInfo)) {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
}
